package com.hhq.sso.protocol.oauth2.entity;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;

/**
 * <p>
 * Description: 单点登录用户
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/18 14:24
 */
@Setter
@Getter
@Entity
@Table(name = "sso_user")
public class SsoUser implements UserDetails {

    /**
     * 用户id
     */
    @Id
    @Column
    private String id;

    /**
     * 租户id
     */
    private String tenantId;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 手机号
     */
    private String mobile;

    /**
     * 电子邮件
     */
    private String email;

    /**
     * 账户是否超时,设置用户的有效期
     */
    private boolean accountNonExpired;

    /**
     * 账户是否锁定
     */
    private boolean accountNonLocked;

    /**
     * 用户是否有效
     */
    private boolean enabled;

    public SsoUser() {}

    public SsoUser(String username, String password, boolean accountNonExpired, boolean accountNonLocked,
        boolean enabled) {
        this.username = username;
        this.password = password;
        this.accountNonExpired = accountNonExpired;
        this.accountNonLocked = accountNonLocked;
        this.enabled = enabled;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public Map<String, Object> info() {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", this.getId());
        claims.put("username", this.getUsername());
        claims.put("mobile", this.getMobile());
        claims.put("email", this.getEmail());
        claims.put("tenantId", this.getTenantId());
        return claims;
    }
}
